Due: Tuesday, September 13, 2016 Finish Chapters 3 in Programming in Scala. Start reading Chapter 4. Although this assignment does not...
Due: Tuesday, September 13, 2016
Finish Chapters 3 in Programming in Scala. Start reading Chapter 4. Although this assignment does not make use of any material in Chapter 4, we will be working with that material in lecture for the next assignment.
Create a project, Hw03, with a main file Hw03.scala. Outside of Eclipse copy the skeletal file Hw03.scala on top of the file generated by Eclipse. You might have to refresh the project for Eclipse to properly notice the new file. Do the following:
Write the unimplemented methods in the skeleton file. Do not use any vars or while loops! Instead, use a purely functional style with vals, recursion, and pattern matching. If you are doing it right you should find that all of the methods are very short.
Elaborate on the test code given so that it exercises all of the methods.
Pick one of the methods (you choose) and implement it using tail recursion. Use the @tailrec annotation on the method so the complier checks that it is actually tail recursive.
Submit your final file Hw03.scala only (put your name in comments). Don't submit the entire project!
////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
/// Hw03 Scala:
package edu.vtc.cis3030
import annotation.tailrec
object Hw03 {
// Returns the smallest f such that f > 1 and f <= n where f divides n evenly.
def findFirstFactor(n: Int): Int = {
@tailrec
def firstFactorFrom(start: Int, n: Int): Int =
if (n % start == 0) start else firstFactorFrom(start + 1, n)
firstFactorFrom(2, n)
}
// Returns a list of n's prime factors.
// For example, factor(12) returns List(2, 2, 3).
def factor(n: Int): List[Int] = ???
// HINT: The first factor f of a number is prime. The remaining factors can be found by
// factoring n/f.
// Factors every number in the given list.
// For example, factorList(List(5, 12, 15)) returns
// List(List(5), List(2, 2, 3), List(3, 5)).
def factorList(numbers: List[Int]): List[List[Int]] = ???
// Converts a String to a list of characters.
// For example, stringToList("Hello") returns List('H', 'e', 'l', 'l', 'o')
def stringToList(s: String): List[Char] = ???
// Removes all negative numbers of a list.
// For example, removeNegatives(List(3, -1, 4)) returns List(3, 4)
def removeNegatives(numbers: List[Int]): List[Int] = ???
// A geometric figure is a triangle if the sum of two side lengths is less than the third for
// all three combinations. The following method removes triples from the list that are NOT
// valid triangles.
// For example, filterTriangles(List((2, 3, 4), (1, 10, 2))) returns List((2, 3, 4))
def filterTriangles(triangles: List[(Int, Int, Int)]): List[(Int, Int, Int)] = ???
// Removes the first n items from a list.
// For example, removeN(2, List(1, 2, 3, 4, 5)) returns List(3, 4, 5)
def removeN[A](n: Int, items: List[A]): List[A] = ???
// Enhance the test program to exercise the other methods above.
def main(args: Array[String]): Unit = {
println(s"factor(12) = ${factor(12)}, expecting: List(2, 2, 3)")
println(s"factor(17) = ${factor(17)}, expecting: List(17)")
println(s"factor(105)= ${factor(105)}, expecting: List(3, 5, 7)")
println(s"factorList(List(5, 12, 15)) = ${factorList(List(5, 12, 15))}, expecting: List(List(5), List(2, 2, 3), List(3, 5))")
}
}
10 years ago
10
- lab report
- The Secret Court and the War on Terror
- Final Lab Report Final Lab Report Final Lab Report You are required to write a complete laboratory report that covers all three experiments for "Lab 2: Water Quality and Contamination," using knowledge gained throughout the course. To begin, download th
- For Asma only
- Psych 660 Individual Programmatic Assessment: Case Study Four Worksheet -- Week Five
- ...THE Integration of Technology...
- Political Science Online Test
- Geico essay
- Due in 6 hours. http://www.homeworkmarket.com/content/read-assignment-well-you-send-me-handshake-its-due-8-hours-now-i-need-perfect-work
- Examining Brand Equity